/**
 * @file ui_top.c
 * @author your name (you@domain.com)
 * @brief top ui
 * @version 0.1
 * @date 2023-06-03
 * 
 * @copyright Copyright (c) 2023
 * 
 */

#include "ui_top.h"
#include "msg_event_config.h"

LV_FONT_DECLAER(font_icon);

static ui_basic_com_t s_ui_top = {0};
static bool s_already_init = false;
static lv_obj_t *bg_obj = NULL;
static lv_obj_t *wifi_img = NULL;
static lv_obj_t *bat_img = NULL;

static void act_gesture_event_cb(lv_event_t *event) 
{
    COMLOG_INFO_LOG("--->\n");
    lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    
    switch(dir) 
    {
        case LV_DIR_LEFT:
             COMLOG_INFO_LOG("LV_DIR_LEFT\n");  
        break;
        case LV_DIR_RIGHT:
             COMLOG_INFO_LOG("LV_DIR_RIGHT\n");  
        break;
        case LV_DIR_TOP:
             COMLOG_INFO_LOG("LV_DIR_TOP\n");   
        break;
        case LV_DIR_BOTTOM:
             COMLOG_INFO_LOG("LV_DIR_BOTTOM\n");   
        break;
        default:
        break;
    }
}

static void top_gesture_init(void)
{
    lv_obj_add_event_cb(bg_obj, act_gesture_event_cb, LV_EVENT_GESTURE, NULL);
}

static int ui_top_enter(int arg0, int arg1, void *arg3)
{
    if(s_already_init)
    {
       return -1;
    }

    s_already_init = true;

    lv_disp_t *d_disp = lv_disp_get_default();
    lv_color_t bg_color = {0};
    bg_color = lv_color_hex(TOP_UI_BG_COLOR);
    bg_obj = lv_obj_create(lv_layer_top());
    lv_obj_set_size(bg_obj, d_disp->driver->hor_res, d_disp->driver->ver_res * TOP_UI_HEIGHT_RATE);
    lv_obj_set_pos(bg_obj, 0, 0);
    lv_obj_set_style_bg_color(bg_obj, bg_color, 0);
    lv_obj_set_style_bg_opa(bg_obj, LV_OPA_60, 0);
    lv_obj_set_style_radius(bg_obj, 0, 0);
    lv_obj_set_style_border_width(bg_obj, 0, 0);
    //disable scorll
    lv_obj_clear_flag(lv_layer_top(), LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_clear_flag(bg_obj, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_clear_flag(bg_obj, LV_OBJ_FLAG_GESTURE_BUBBLE);
    // lv_obj_add_flag(bg_obj,  LV_OBJ_FLAG_CLICKABLE);
    //lv_obj_set_flex_flow(bg_obj, LV_FLEX_FLOW_ROW);
    //lv_obj_set_style_flex_flow(bg_obj, LV_FLEX_ALIGN_CENTER, 0);

    //battery dispaly
    bat_img = lv_img_create(bg_obj);  
    lv_img_set_src(bat_img,  LV_SYMBOL_BATTERY_FULL);
    lv_obj_align(bat_img, LV_ALIGN_RIGHT_MID, 0, 0); 
    lv_obj_set_style_bg_color(bat_img, lv_color_hex(0xffffff), 0);  

    //wifi display
    wifi_img = lv_label_create(bg_obj);  
    lv_img_set_src(wifi_img,  LV_SYMBOL_WIFI);
    lv_obj_align_to(wifi_img, bat_img, LV_ALIGN_OUT_LEFT_MID, -10, 0);   

    //gesture
    //top_gesture_init();   
    return 0;
}

static int ui_top_loop(int arg0, int arg1, void *arg3)
{
    if(!s_already_init)
    {
       return -1;
    }

    return 0;
}

static int ui_top_exit(int arg0, int arg1, void *arg3)
{
    if(!s_already_init)
    {
       return -1;
    }

    s_already_init = false;

    lv_obj_del(bg_obj);

    return 0;
}

static void ui_top_event(char *event_name, void *data, int srcId)
{
    if(strcmp(event_name, TOPIC_BAT_LVL) == 0)
    {
       if(bat_img)
       {
          int bat_lvl = 0;
          memcpy(&bat_lvl, data, sizeof(int));
          switch(bat_lvl)
          {
            case 0: lv_img_set_src(bat_img,  LV_SYMBOL_BATTERY_EMPTY); break;
            case 1: lv_img_set_src(bat_img,  LV_SYMBOL_BATTERY_1); break;
            case 2: lv_img_set_src(bat_img,  LV_SYMBOL_BATTERY_2); break;
            case 3: lv_img_set_src(bat_img,  LV_SYMBOL_BATTERY_3); break;
            case 4: lv_img_set_src(bat_img,  LV_SYMBOL_BATTERY_FULL); break;
            default: break;
          }
       }
    }
}

ui_basic_com_t *ui_top_create(void)
{
    
    easy_ui_sub_event(TOPIC_BAT_LVL, ui_top_event, ANY_ID);  

    s_ui_top.ui_id = EASY_UI_TOP;
    memcpy(s_ui_top.ui_name, EASY_UI_NAME(EASY_UI_TOP), strlen(EASY_UI_NAME(EASY_UI_TOP)));
    s_ui_top.enter = ui_top_enter;
    s_ui_top.run = ui_top_loop;
    s_ui_top.exit = ui_top_exit;
    return &s_ui_top;
}